package test.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yachao.guo
 * @date 2019-08-26 18:07
 */
public class ReadFile {

    public static void readFileByLines(String readfileName,String writeFileName) {
        File file = new File(readfileName);
        File wfile = new File(writeFileName);

        try(BufferedReader reader = new BufferedReader(new FileReader(file));
            BufferedWriter writer = new BufferedWriter(new FileWriter(wfile))){
            System.out.println("以行为单位读取文件内容，一次读一整行：");

            String tempString = null;
            int line = 1;
            int id = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                //System.out.println(tempString);
                String[] str = tempString.split("\\|");
                for (int i=0;i<str.length;i++){

                    String sql = String.format("insert into `word_gid`(id, word, gid, ezone_shard_info, "
                        + "drc_check_time) values (%s, '%s', %s, null, '2018-12-21 23:34:07.381');\n",id,filterSpacialCharacter(str[i]),line);
                    writer.write(sql);
                    id++;
                }
                // 显示行号
                //System.out.println("line " + line + ": " + tempString);
                line++;
            }
            System.out.println("执行完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static String filterSpacialCharacter(String str) {
        String regEx = "[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern pattern = Pattern.compile(regEx);
        Matcher m = pattern.matcher(str);
        return m.replaceAll("").trim();
    }
    public static void main(String[] args) {
        String readFileName = "/Users/guoyachao/Downloads/syn.txt";
        String writeFileName = "/Users/guoyachao/Downloads/syn.sql";
        readFileByLines(readFileName,writeFileName);
    }



}

